﻿// -------------------------------------------------------------------------
//    @FileName         :    NFTransCreateUserDetail.cpp
//    @Author           :    xxxxx
//    @Date             :   xxxx-xx-xx
//    @Email			:    xxxxxxxxx@xxx.xxx
//    @Module           :    NFTransCreateUserDetail.cpp
//
// -------------------------------------------------------------------------

#include "NFTransPlayerBase.h"

#include <NFPacketSendUtil.h>
#include <NFServerUtil.h>
#include <NFSessionUtil.h>
#include <NFComm/NFCore/NFTimeUtility.h>
#include <NFLogicCommon/NFScopedMessageHead.h>

#include "NFComm/NFPluginModule/NFLogMgr.h"
#include "NFComm/NFPluginModule/NFCheck.h"
#include "NFLogicCommon/NFLogicShmTypeDefines.h"
#include "NFComm/NFPluginModule/NFICoroutineModule.h"
#include "NFPlayer.h"
#include "NFPlayerMgr.h"
#include "NFSession.h"
#include "NFSysMsgUtil.h"
#include "proto_cs_cmd.nanopb.h"

NFTransPlayerBase::NFTransPlayerBase()
{
    if (NFShmMgr::Instance()->GetCreateMode() == EN_OBJ_MODE_INIT)
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFTransPlayerBase::~NFTransPlayerBase()
{
}

int NFTransPlayerBase::CreateInit()
{
    m_ullRoleID = 0;
    m_fromBusId = 0;
    m_bHasIncreasedCount = false;
    m_reqTransId = 0;
    m_iPlayerObjID = 0;
    m_iClientSessionID = 0;
    return 0;
}

int NFTransPlayerBase::ResumeInit()
{
    return 0;
}

int NFTransPlayerBase::Init(NFPlayer* pPlayer, uint32_t cmd, uint32_t csSeq, uint32_t fromBusId, uint32_t reqTransId)
{
    CHECK_NULL(0, pPlayer);
    NFTransBase::Init();

    m_stCSMsgHeadReq.m_iMsgID = (proto_ff::Proto_CS_CMD)cmd;
    m_stCSMsgHeadReq.m_iSeqCS = csSeq;

    m_ullRoleID = pPlayer->GetRoleID();
    m_iPlayerObjID = pPlayer->GetGlobalId();
    m_iClientSessionID = pPlayer->GetClientSessionID();
    m_fromBusId = fromBusId;
    m_reqTransId = reqTransId;

    m_stCSMsgHeadReq.m_iSeqCS = NFScopedMessageHead::GetStackRequestSeq(m_iClientSessionID, m_stCSMsgHeadReq.m_iSeqCS);

    if (!m_bHasIncreasedCount)
    {
        pPlayer->IncreaseTransNum();
        m_bHasIncreasedCount = true;
    }
    return 0;
}

int NFTransPlayerBase::Init(NFSession* pSession, uint32_t cmd, uint32_t csSeq, uint32_t fromBusId, uint32_t reqTransId)
{
    CHECK_NULL(0, pSession);
    SetClientSession(pSession);

    m_stCSMsgHeadReq.m_iMsgID = (proto_ff::Proto_CS_CMD)cmd;
    m_stCSMsgHeadReq.m_iSeqCS = csSeq;

    m_fromBusId = fromBusId;
    m_reqTransId = reqTransId;

    m_stCSMsgHeadReq.m_iSeqCS = NFScopedMessageHead::GetStackRequestSeq(m_iClientSessionID, m_stCSMsgHeadReq.m_iSeqCS);

    return 0;
}

int NFTransPlayerBase::SetClientSession(NFSession* pSession)
{
    if (pSession)
    {
        m_iClientSessionID = pSession->GetGlobalId();
        NFPlayer* pPlayer = pSession->GetPlayer();
        if (pPlayer)
        {
            m_ullRoleID = pPlayer->GetRoleID();
            m_iPlayerObjID = pPlayer->GetGlobalId();
        }
    }
    else
    {
        m_iClientSessionID = INVALID_ID;

        return -1;
    }

    return 0;
}

NFSession* NFTransPlayerBase::GetClientSession() const
{
    return NFSessionUtil::GetSessionObj(m_iClientSessionID);
}

NFPlayer* NFTransPlayerBase::Player()
{
    return NFPlayer::GetObjByGlobalId(m_iPlayerObjID);
}

NFPlayer* NFTransPlayerBase::GetPlayer()
{
    return NFPlayer::GetObjByGlobalId(m_iPlayerObjID);
}

int NFTransPlayerBase::OnTimeOut()
{
    NFPlayer* pPlayer = GetPlayer();
    if (pPlayer && m_bHasIncreasedCount)
    {
        pPlayer->DecreaseTransNum();
        m_bHasIncreasedCount = false;
    }

    SetFinished(NFrame::ERR_CODE_SVR_SYSTEM_TIMEOUT);

    NFLogError(NF_LOG_DEFAULT, m_ullRoleID, "NFTransLogicUserBase timeout, playerId:{}, cmd:{}, csseq:{} transid:{}", m_ullRoleID, m_stCSMsgHeadReq.m_iMsgID, m_stCSMsgHeadReq.m_iSeqCS, m_reqTransId);
    return 0;
}

int NFTransPlayerBase::OnTransFinished(int iRunLogicRetCode)
{
    NFPlayer* pPlayer = GetPlayer();
    if (!pPlayer)
    {
        NFLogInfo(NF_LOG_DEFAULT, m_ullRoleID, "GetPlayer Failed, this player may off line! cmd:{}", m_stCSMsgHeadReq.m_iMsgID);
        return 0;
    }

    if (m_bHasIncreasedCount)
    {
        pPlayer->DecreaseTransNum();
        m_bHasIncreasedCount = false;
    }

    if (0 == iRunLogicRetCode)
    {
        return 0;
    }

    NFLogDebug(NF_LOG_DEFAULT, pPlayer->GetRoleID(), "player:{} failed errcode={}, msgid={} csSeq:{} globalid={} classtype:{}", pPlayer->GetRoleID(), iRunLogicRetCode, m_stCSMsgHeadReq.m_iMsgID, m_stCSMsgHeadReq.m_iSeqCS, GetGlobalId(), GetClassType());

    if (0 == m_stCSMsgHeadReq.m_iMsgID)
    {
        NFLogError(NF_LOG_DEFAULT, pPlayer->GetRoleID(), "Server should not call this function because request head uninitialized, player:{}", pPlayer->GetRoleID());

        return -1;
    }

    if (iRunLogicRetCode > 0)
    {
#ifdef NF_DEBUG_MODE
        std::string errMsg = NFLogMgr::Instance()->GetErrorInfo();
        NFSysMsgUtil::SendSysMsg(pPlayer, proto_ff::E_SysMsg_Show_Type_B, 100, errMsg);
#endif
        if (proto_ff::Proto_CS_ErrorCode_IsValid(iRunLogicRetCode))
        {
            int iRet = NFPacketSendUtil::SendErrToClient(pPlayer, m_stCSMsgHeadReq.m_iMsgID, iRunLogicRetCode);
            CHECK_PLAYER_RET(pPlayer->GetRoleID(), iRet, "Send Message to player=[{}] failed", pPlayer->GetRoleID());
        }
        else
        {
            int iRet = NFPacketSendUtil::SendErrToClient(pPlayer, m_stCSMsgHeadReq.m_iMsgID, ERR_CODE_SYSTEM_ERROR);
            CHECK_PLAYER_RET(pPlayer->GetRoleID(), iRet, "Send Message to player=[{}] failed", pPlayer->GetRoleID());
        }
    }

    return 0;
}

void NFTransPlayerBase::ClearClientSessionAndPlayer()
{
    m_ullRoleID = 0;
    m_iClientSessionID = INVALID_ID;
    m_iPlayerObjID = INVALID_ID;
}

int NFTransPlayerBase::HandleTransFinished(int iRunLogicRetCode)
{
    return 0;
}

std::string NFTransPlayerBase::GetDebugInfo() const
{
    std::string log = NF_FORMAT("ThisGlobalID:{} ClientSessionID:{} CurState:{} StartTime:{} RunedTimes:{} PlayerObjID:{} roleID:{}", GetGlobalId(), m_iClientSessionID, m_wCurState, NFTimeUtility::DateTimeToStr(m_dwStartTime), m_wRunedTimes, m_iPlayerObjID, m_ullRoleID);
    NFSession* pSession = GetClientSession();
    if (pSession)
    {
        log += " SessionInfo:" + pSession->GetDebugInfo();
    }
    return log;
}
